/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * Copyright (C) 2019 Tianjin KYLIN Information Technology Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
#include "notice.h"
#include "appdetail.h"
#include "realizenotice.h"
#include "noticemenu.h"
#include "widgets/HoverWidget/hoverwidget.h"
#include <QFileDialog>
#include <QTimer>
#include <QFileSystemWatcher>
#include <QSettings>
#include <QToolButton>

#define NOTICE_SCHEMA         "org.ukui.control-center.notice"
#define NEW_FEATURE_KEY       "show-new-feature"
#define ENABLE_NOTICE_KEY     "enable-notice"
#define SHOWON_LOCKSCREEN_KEY "show-on-lockscreen"

#define DESKTOPPATH           "/usr/share/applications/"
#define DESKTOPOTHERPAYH      "/etc/xdg/autostart/"

Notice::Notice() : mFirstLoad(true)
{
    pluginName = tr("Notice");
    pluginType = SYSTEM;
}

Notice::~Notice()
{
    if (!mFirstLoad) {
        delete mstringlist;
        mstringlist = nullptr;
        qDeleteAll(vecGsettins);
        vecGsettins.clear();
    }
}

QString Notice::plugini18nName()
{
    return pluginName;
}

int Notice::pluginTypes()
{
    return pluginType;
}

QWidget *Notice::pluginUi()
{
    if (mFirstLoad) {
        pluginWidget = new QWidget;
        pluginWidget->setAttribute(Qt::WA_DeleteOnClose);
        mFirstLoad = false;

        if (QGSettings::isSchemaInstalled(NOTICE_SCHEMA)) {
            QByteArray id(NOTICE_SCHEMA);
            nSetting = new QGSettings(id, QByteArray(), this);
        }
        if (QGSettings::isSchemaInstalled(THEME_QT_SCHEMA)) {
            QByteArray id(THEME_QT_SCHEMA);
            mThemeSetting = new QGSettings(id, QByteArray(), this);
        }

        if (QGSettings::isSchemaInstalled(NOTIFICATION_DEMO_SCHEMA)) {
            QByteArray id(NOTIFICATION_DEMO_SCHEMA);
            mDisTrubSettings = new QGSettings(id, QByteArray(), this);
        }

        initUi(pluginWidget);
        //获取已经存在的动态路径
        listChar = listExistsCustomNoticePath();
        mstringlist = new QStringList();

        initSearchText();
        setupComponent();
        setupGSettings();
        initNoticeStatus();

        //设置白名单
        whitelist.append("kylin-screenshot.desktop");
        whitelist.append("peony.desktop");
        whitelist.append("kylin-nm.desktop");
        whitelist.append("ukui-flash-disk.desktop");
        whitelist.append("ukui-powermanagement-daemon.desktop");
        whitelist.append("kylin-system-update.desktop");
        whitelist.append("ukui-bluetooth.desktop");
        initOriNoticeStatus();

        //监视desktop文件列表
        QFileSystemWatcher *m_fileWatcher=new QFileSystemWatcher;
        m_fileWatcher->addPaths(QStringList()<<QString(DESKTOPPATH));
        //有应用卸载或安装时
        connect(m_fileWatcher,&QFileSystemWatcher::directoryChanged,[=](){
            loadlist();
        });

    }
    return pluginWidget;
}

const QString Notice::name() const
{
    return QStringLiteral("Notice");
}

bool Notice::isShowOnHomePage() const
{
    return false;
}

QIcon Notice::icon() const
{
    return QIcon::fromTheme("ukui-tool-symbolic");
}

bool Notice::isEnable() const
{
    return true;
}

void Notice::initUi(QWidget *widget)
{
    QVBoxLayout *mverticalLayout = new QVBoxLayout(widget);
    mverticalLayout->setSpacing(0);
    mverticalLayout->setContentsMargins(0, 0, 0, 0);

    QFrame *notFazeFrame = new QFrame(widget);
    initNotFaze(notFazeFrame);

    QWidget *Noticewidget = new QWidget(widget);
    Noticewidget->setMinimumSize(QSize(550, 0));
    Noticewidget->setMaximumSize(QSize(16777215, 16777215));

    QVBoxLayout *NoticeLayout = new QVBoxLayout(Noticewidget);
    NoticeLayout->setContentsMargins(0, 0, 0, 0);
    NoticeLayout->setSpacing(8);

    mNoticeLabel = new TitleLabel(Noticewidget);
    mNotFazeLabel = new TitleLabel();
    mNotFazeLabel->setText(tr("NotFaze Mode"));

    LightLabel *notFazeLabel = new LightLabel(tr("(Notification banners, prompts will be hidden, and notification sounds will be muted)"));
    QFrame *distrubFrame = new QFrame(widget);
    distrubFrame->setMinimumWidth(550);
    distrubFrame->setMaximumWidth(16777215);

    QHBoxLayout *distrubLyt = new QHBoxLayout(distrubFrame);
    distrubLyt->setContentsMargins(0, 0, 0, 0);
    distrubLyt->addWidget(mNotFazeLabel);
    distrubLyt->addSpacing(4);
    distrubLyt->addWidget(notFazeLabel, Qt::AlignLeft);

    mGetNoticeFrame  = new QFrame(Noticewidget);
    mGetNoticeFrame->setMinimumSize(QSize(550, 60));
    mGetNoticeFrame->setMaximumSize(QSize(16777215, 60));
    mGetNoticeFrame->setFrameShape(QFrame::Box);

    QHBoxLayout *mGetNoticeLayout = new QHBoxLayout(mGetNoticeFrame);
    mGetNoticeLayout->setContentsMargins(16,0,16,0);

    mGetNoticeLabel = new QLabel(mGetNoticeFrame);
    mGetNoticeLabel->setFixedWidth(550);
    enableSwitchBtn = new KSwitchButton(mGetNoticeFrame);

    mGetNoticeLayout->addWidget(mGetNoticeLabel,Qt::AlignLeft);
    mGetNoticeLayout->addStretch();
    mGetNoticeLayout->addWidget(enableSwitchBtn);

    mNoticeAppFrame = new QFrame(Noticewidget);
    mNoticeAppFrame->setMinimumSize(QSize(550, 0));
    mNoticeAppFrame->setMaximumSize(QSize(16777215, 16777215));
    mNoticeAppFrame->setFrameShape(QFrame::Box);

    applistverticalLayout = new QVBoxLayout(mNoticeAppFrame);
    applistverticalLayout->setContentsMargins(0,0,0,0);
    applistverticalLayout->setSpacing(0);

    NoticeLayout->addWidget(mNoticeLabel);
    NoticeLayout->addWidget(mGetNoticeFrame);
    NoticeLayout->addWidget(mNoticeAppFrame);

    if (mDisTrubSettings && mDisTrubSettings->keys().contains(FULLSCREEN_KEY)) {
//        mverticalLayout->addLayout(distrubLyt);
        mverticalLayout->addWidget(distrubFrame);
        mverticalLayout->addSpacing(8);
        mverticalLayout->addWidget(notFazeFrame);
        mverticalLayout->addSpacing(32);
    } else {
        distrubFrame->hide();
        notFazeFrame->hide();
    }
    mverticalLayout->addWidget(Noticewidget);
    mverticalLayout->addStretch();

}

void Notice::initNotFaze(QFrame *frame)
{
    frame->setMinimumSize(QSize(550, 0));
    frame->setMaximumSize(QSize(16777215, 16777215));
    frame->setFrameShape(QFrame::Box);

    QVBoxLayout *notFazeLyt = new QVBoxLayout(frame);
    notFazeLyt->setContentsMargins(0, 0, 0, 0);
    notFazeLyt->setSpacing(0);

    QFrame *line_1 = NoticeMenu::setLine();
    QFrame *line_2 = NoticeMenu::setLine();
    QFrame *line_3 = NoticeMenu::setLine();

    QFrame *autoOpenFrame = new QFrame(frame);
    autoOpenFrame->setMinimumSize(550, 60);
    autoOpenFrame->setMaximumSize(16777215, 60);
    QHBoxLayout *autoLyt = new QHBoxLayout(autoOpenFrame);
    autoLyt->setContentsMargins(16, 0, 16, 0);
    QLabel *autoOpenLabel = new QLabel(tr("Automatically turn on"), autoOpenFrame);
    mOpenTimeHCombox = new QComboBox(autoOpenFrame);
    mOpenTimeHCombox->setFixedWidth(64);
    mOpenTimeMCombox = new QComboBox(autoOpenFrame);
    mOpenTimeMCombox->setFixedWidth(64);
    mCloseTimeHCombox = new QComboBox(autoOpenFrame);
    mCloseTimeHCombox->setFixedWidth(64);
    mCloseTimeMCombox = new QComboBox(autoOpenFrame);
    mCloseTimeMCombox->setFixedWidth(64);
    for (int i = 0; i < 24; i++) {
        mOpenTimeHCombox->addItem(QStringLiteral("%1").arg(i, 2, 10, QLatin1Char('0')));
        mCloseTimeHCombox->addItem(QStringLiteral("%1").arg(i, 2, 10, QLatin1Char('0')));
    }

    for (int i = 0; i < 60; i++) {
        mOpenTimeMCombox->addItem(QStringLiteral("%1").arg(i, 2, 10, QLatin1Char('0')));
        mCloseTimeMCombox->addItem(QStringLiteral("%1").arg(i, 2, 10, QLatin1Char('0')));
    }

    QLabel * label_1 = new QLabel(autoOpenFrame);
    label_1->setFixedWidth(20);
    label_1->setText(tr("to"));
    QLabel *label_2 = new QLabel(autoOpenFrame);
    label_2->setFixedWidth(4);
    label_2->setText(":");
    QLabel *label_3 = new QLabel(autoOpenFrame);
    label_3->setFixedWidth(4);
    label_3->setText(":");
    mAutoOpenSwitchBtn = new KSwitchButton(autoOpenFrame);

    autoLyt->addWidget(autoOpenLabel);
    autoLyt->addStretch();
    autoLyt->addWidget(mOpenTimeHCombox);
    autoLyt->addWidget(label_2);
    autoLyt->addWidget(mOpenTimeMCombox);
    autoLyt->addWidget(label_1);
    autoLyt->addWidget(mCloseTimeHCombox);
    autoLyt->addWidget(label_3);
    autoLyt->addWidget(mCloseTimeMCombox);
    autoLyt->addSpacing(24);
    autoLyt->addWidget(mAutoOpenSwitchBtn);

    QFrame *multiScreenFrame = new QFrame(frame);
    mMultiScreenSwitchBtn= new KSwitchButton();
    setFrame(multiScreenFrame, mMultiScreenSwitchBtn, tr("Automatically turn on when multiple screens are connected"));

    QFrame *fullScreenFrame = new QFrame(frame);
    mFullScreenSwitchBtn= new KSwitchButton();
    setFrame(fullScreenFrame, mFullScreenSwitchBtn, tr("Automatically open in full screen mode"));

    QFrame *allowAlarmrRemindersFrame = new QFrame(frame);
    mAllowAlarmrSwitchBtn= new KSwitchButton();
    setFrame(allowAlarmrRemindersFrame, mAllowAlarmrSwitchBtn, tr("Allow automatic alarm reminders in Do Not Disturb mode"));

    notFazeLyt->addWidget(autoOpenFrame);
    notFazeLyt->addWidget(line_1);
    notFazeLyt->addWidget(multiScreenFrame);
    notFazeLyt->addWidget(line_2);
    notFazeLyt->addWidget(fullScreenFrame);
    notFazeLyt->addWidget(line_3);
    notFazeLyt->addWidget(allowAlarmrRemindersFrame);
}

void Notice::setFrame(QFrame *frame, KSwitchButton *btn, QString str)
{
    frame->setMinimumSize(QSize(550, 60));
    frame->setMaximumSize(QSize(16777215, 60));
    frame->setFrameShape(QFrame::NoFrame);

    QHBoxLayout *hLyt = new QHBoxLayout(frame);
    hLyt->setContentsMargins(16, 0, 16, 0);
    QLabel *label = new QLabel(str, frame);

    hLyt->addWidget(label);
    hLyt->addStretch();
    hLyt->addWidget(btn);

}


void Notice::initSearchText()
{
    mNoticeLabel->setText(tr("Notice Settings"));
    //~ contents_path /notice/Get notifications from the app
    mGetNoticeLabel->setText(tr("Get notifications from the app"));
}

void Notice::setupComponent()
{
    if (mDisTrubSettings) {
        if (mDisTrubSettings->keys().contains(AUTOSTART_KEY)) {
            bool ischecked = mDisTrubSettings->get(AUTOSTART_KEY).toBool();
            mAutoOpenSwitchBtn->setChecked(ischecked);
            if (mDisTrubSettings->keys().contains(TIMESTART_KEY) && mDisTrubSettings->keys().contains(TIMEEND_KEY)) {
                QString starttime = mDisTrubSettings->get(TIMESTART_KEY).toString();
                QString endtime = mDisTrubSettings->get(TIMEEND_KEY).toString();
                QStringList startlist = starttime.split(":");
                QStringList endlist = endtime.split(":");
                if (startlist.length() >= 2 && endlist.length() >= 2) {
                    mOpenTimeHCombox->setCurrentText(startlist.at(0));
                    mOpenTimeMCombox->setCurrentText(startlist.at(1));
                    mCloseTimeHCombox->setCurrentText(endlist.at(0));
                    mCloseTimeMCombox->setCurrentText(endlist.at(1));
                }
                setComBoxStatus(ischecked);
            }
        }

        if (mDisTrubSettings->keys().contains(PROJECTIONSCREEN_KEY))
            mMultiScreenSwitchBtn->setChecked(mDisTrubSettings->get(PROJECTIONSCREEN_KEY).toBool());

        if (mDisTrubSettings->keys().contains(FULLSCREEN_KEY))
            mFullScreenSwitchBtn->setChecked(mDisTrubSettings->get(FULLSCREEN_KEY).toBool());

        if (mDisTrubSettings->keys().contains(ALARMCLOCK_KEY))
            mAllowAlarmrSwitchBtn->setChecked(mDisTrubSettings->get(ALARMCLOCK_KEY).toBool());
    }
}

void Notice::setupGSettings()
{
    connect(mThemeSetting, &QGSettings::changed, [=](const QString &key){
        if (key == "iconThemeName")
            loadlist();
    });

    connect(enableSwitchBtn, &KSwitchButton::stateChanged, [=](bool checked){
        nSetting->set(ENABLE_NOTICE_KEY, checked);
        setHiddenNoticeApp(checked);
    });

    if (mDisTrubSettings) {
        if (mDisTrubSettings->keys().contains(AUTOSTART_KEY)) {
            connect(mAutoOpenSwitchBtn, &KSwitchButton::stateChanged, [=](bool checked) {
                mDisTrubSettings->set(AUTOSTART_KEY, checked);
                setComBoxStatus(checked);
            });
        }

        if (mDisTrubSettings->keys().contains(PROJECTIONSCREEN_KEY)) {
            connect(mMultiScreenSwitchBtn, &KSwitchButton::stateChanged, [=](bool checked) {
                mDisTrubSettings->set(PROJECTIONSCREEN_KEY, checked);
            });
        }

        if (mDisTrubSettings->keys().contains(FULLSCREEN_KEY)) {
            connect(mFullScreenSwitchBtn, &KSwitchButton::stateChanged, [=](bool checked) {
                mDisTrubSettings->set(FULLSCREEN_KEY, checked);
            });
        }

        if (mDisTrubSettings->keys().contains(ALARMCLOCK_KEY)) {
            connect(mAllowAlarmrSwitchBtn, &KSwitchButton::stateChanged, [=](bool checked) {
                mDisTrubSettings->set(ALARMCLOCK_KEY, checked);
            });
        }

        if (mDisTrubSettings->keys().contains(TIMESTART_KEY) && mDisTrubSettings->keys().contains(TIMEEND_KEY)) {
            connect(mOpenTimeHCombox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index){
                Q_UNUSED(index);
                mDisTrubSettings->set(TIMESTART_KEY, QString("%1%2%3").arg(mOpenTimeHCombox->currentText()).arg(":").arg(mOpenTimeMCombox->currentText()));
                qDebug()<<mDisTrubSettings->get(TIMESTART_KEY).toString();
            });

            connect(mOpenTimeMCombox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index){
                Q_UNUSED(index);
                mDisTrubSettings->set(TIMESTART_KEY, QString("%1%2%3").arg(mOpenTimeHCombox->currentText()).arg(":").arg(mOpenTimeMCombox->currentText()));
                qDebug()<<mDisTrubSettings->get(TIMESTART_KEY).toString();
            });

            connect(mCloseTimeHCombox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index){
                Q_UNUSED(index);
                mDisTrubSettings->set(TIMEEND_KEY, QString("%1%2%3").arg(mCloseTimeHCombox->currentText()).arg(":").arg(mCloseTimeMCombox->currentText()));
                qDebug()<<mDisTrubSettings->get(TIMEEND_KEY).toString();
            });

            connect(mCloseTimeMCombox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index){
                Q_UNUSED(index);
                mDisTrubSettings->set(TIMEEND_KEY, QString("%1%2%3").arg(mCloseTimeHCombox->currentText()).arg(":").arg(mCloseTimeMCombox->currentText()));
                qDebug()<<mDisTrubSettings->get(TIMEEND_KEY).toString();
            });
        }
    }
}

void Notice::initNoticeStatus()
{
    enableSwitchBtn->blockSignals(true);
    enableSwitchBtn->setChecked(nSetting->get(ENABLE_NOTICE_KEY).toBool());
    enableSwitchBtn->blockSignals(false);

//    isCN_env = nSetting->get(IS_CN).toBool();
    mlocale = QLocale::system().name();
    if (mlocale == "zh_CN") {
        mEnv = true;
    } else {
        mEnv = false;
    }
    setHiddenNoticeApp(enableSwitchBtn->isChecked());

}

void Notice::initOriNoticeStatus()
{
    QDir dir(QString(DESKTOPPATH).toUtf8());
    QDir otherdir(QDir::homePath() + "/.local/share/applications");
    QDir autodir(QString(DESKTOPOTHERPAYH).toUtf8());

    QStringList filters;
    filters<<QString("*.desktop");
    dir.setFilter(QDir::Files | QDir::NoSymLinks); // 设置类型过滤器，只为文件格式
    otherdir.setFilter(QDir::Files | QDir::NoSymLinks);
    autodir.setFilter(QDir::Files | QDir::NoSymLinks);

    dir.setNameFilters(filters);  // 设置文件名称过滤器，只为filters格式
    otherdir.setNameFilters(filters);
    autodir.setNameFilters(filters);

    //初始化列表界面
    initListUI(dir,DESKTOPPATH,mstringlist);
    initListUI(autodir,DESKTOPOTHERPAYH,mstringlist);
    mstringlist->clear();

}

void Notice::initListUI(QDir dir,QString mpath,QStringList *stringlist)
{
    for (uint i = 0; i < dir.count(); i++) {
        QString file_name = dir[i];  // 文件名称
        if (!whitelist.contains(file_name)) {
            continue;
        }
        count++;
        QSettings* desktopFile = new QSettings(mpath+file_name, QSettings::IniFormat);
        QString no_display,not_showin,only_showin,appname,appname_CN,appname_US,icon;
        if (desktopFile) {
           desktopFile->setIniCodec("utf-8");

           no_display = desktopFile->value(QString("Desktop Entry/NoDisplay")).toString();
           not_showin = desktopFile->value(QString("Desktop Entry/NotShowIn")).toString();
           only_showin = desktopFile->value(QString("Desktop Entry/OnlyShowIn")).toString();
           icon = desktopFile->value(QString("Desktop Entry/Icon")).toString();
           appname = desktopFile->value(QString("Desktop Entry/Name")).toString();
           appname_CN = desktopFile->value(QString("Desktop Entry/Name[zh_CN]")).toString();
           appname_US = desktopFile->value(QString("Desktop Entry/Name")).toString();
           delete desktopFile;
           desktopFile = nullptr;
        }

        if (not_showin != nullptr) {
            if (not_showin.contains("UKUI")) {
                continue;
            }
        }
        if (only_showin != nullptr) {
            if (only_showin.contains("LXQt") || only_showin.contains("KDE")) {
                continue;
            }
        }
        if (stringlist->contains(appname)) {
            qDebug()<<appname;
            continue;
        }
        stringlist->append(appname);
        // 构建Widget

        QFrame *baseWidget = new QFrame(mNoticeAppFrame);
        baseWidget->setMinimumWidth(550);
        baseWidget->setMaximumWidth(16777215);
        baseWidget->setFixedHeight(60);
        baseWidget->setFrameShape(QFrame::Shape::NoFrame);
        baseWidget->setAttribute(Qt::WA_DeleteOnClose);

        QPushButton *iconBtn = new QPushButton(baseWidget);

        iconBtn->setStyleSheet("QPushButton{background-color:transparent;border-radius:4px}"
                               "QPushButton:hover{background-color: transparent ;color:transparent;}");
        iconBtn->setIconSize(QSize(32, 32));
        iconBtn->setIcon(QIcon::fromTheme(icon,
                                          QIcon(QString("/usr/share/pixmaps/"+icon)
                                                        +".png")));

        QHBoxLayout *devHorLayout = new QHBoxLayout(baseWidget);
        devHorLayout->setSpacing(8);
        devHorLayout->setContentsMargins(16, 0, 16, 0);


        QLabel *nameLabel = new QLabel(baseWidget);

        QToolButton *setBtn = new QToolButton(baseWidget);
        setBtn->setStyleSheet("QToolButton:!checked{background-color: palette(base)}");
        setBtn->setProperty("useButtonPalette", true);
        setBtn->setPopupMode(QToolButton::InstantPopup);
        setBtn->setFixedSize(QSize(36, 36));
        setBtn->setIcon(QIcon::fromTheme("view-more-horizontal-symbolic"));

        NoticeMenu *menu = new NoticeMenu(setBtn);
        setBtn->setMenu(menu);

        KSwitchButton *appSwitch = new KSwitchButton(baseWidget);

        devHorLayout->addWidget(iconBtn);
        devHorLayout->addWidget(nameLabel);
        devHorLayout->addStretch();
        devHorLayout->addWidget(setBtn);
        devHorLayout->addWidget(appSwitch);

        QFrame *line = new QFrame(pluginWidget);
        line->setMinimumSize(QSize(0, 1));
        line->setMaximumSize(QSize(16777215, 1));
        line->setLineWidth(0);
        line->setFrameShape(QFrame::HLine);
        line->setFrameShadow(QFrame::Sunken);

        applistverticalLayout->addWidget(baseWidget);
        applistverticalLayout->addWidget(line);
        if (count == whitelist.count()) {
            line->hide();
        }

        //创建gsettings对象
        if(appname_CN.isEmpty()) {
            appname_CN = appname_US;
        }

        const QByteArray id(NOTICE_ORIGIN_SCHEMA);
        QGSettings *cnSettings = nullptr;
        QGSettings *usSettings = nullptr;

        QString cnpath = QString("%1%2%3").arg(NOTICE_ORIGIN_PATH).arg(appname_CN).arg("/");
        cnSettings = new QGSettings(id, cnpath.toUtf8().data(), this);

        QString uspath = QString("%1%2%3").arg(NOTICE_ORIGIN_PATH).arg(appname_US).arg("/");
        usSettings = new QGSettings(id, uspath.toUtf8().data(), this);

        QGSettings *settings = nullptr;
        if (mEnv) {
            settings = cnSettings;
            nameLabel->setText(appname_CN);
        } else {
            settings = usSettings;
            nameLabel->setText(appname_US);
        }

        bool isCheck = settings->get(MESSAGES_KEY).toBool();
        appSwitch->setChecked(isCheck);
        bool voiceEnable = settings->get(VOICE_KEY).toBool();
        bool detailEnable = settings->get(SHOW_DETAIL_KEY).toBool();
        bool showEnable = settings->get(SHOW_SCREENLOCK_KEY).toBool();
        QString style = settings->get(STYLE_KEY).toString();
         int mode = 0;
         if (style == "mutative") {
             mode = 0;
         } else if (style == "always") {
             mode = 1;
         } else if (style == "none") {
             mode = 2;
         }
        menu->initStaus(voiceEnable, detailEnable, showEnable, mode);

        connect(menu, &NoticeMenu::voiceSignals, [=](bool checked) {
            cnSettings->set(VOICE_KEY, checked);
            usSettings->set(VOICE_KEY, checked);
        });

        connect(menu, &NoticeMenu::detailSignals, [=](bool checked) {
            cnSettings->set(SHOW_DETAIL_KEY, checked);
            usSettings->set(SHOW_DETAIL_KEY, checked);
        });

        connect(menu, &NoticeMenu::showSignals, [=](bool checked) {
            cnSettings->set(SHOW_SCREENLOCK_KEY, checked);
            usSettings->set(SHOW_SCREENLOCK_KEY, checked);
        });

        connect(menu, &NoticeMenu::styleBtnSignals, [=](int id) {
            QString str("mutative");
            if (id == 0)
                str = "mutative";
            else if (id == 1)
                str = "always";
            else if (id == 2)
                str = "none";
            cnSettings->set(STYLE_KEY, str);
            usSettings->set(STYLE_KEY, str);
        });


        connect(cnSettings, &QGSettings::changed, [=](QString key) {
            if (static_cast<QString>(MESSAGES_KEY) == key) {
                appSwitch->blockSignals(true);
                bool judge = cnSettings->get(MESSAGES_KEY).toBool();
                if (usSettings->get(MESSAGES_KEY).toBool() != judge)
                    usSettings->set(MESSAGES_KEY, judge);
                if (appSwitch->isChecked() != judge)
                    appSwitch->setChecked(judge);
                appSwitch->blockSignals(false);
            }
        });

        connect(usSettings, &QGSettings::changed, [=](QString key) {
            if (static_cast<QString>(MESSAGES_KEY) == key) {
                appSwitch->blockSignals(true);
                bool judge = usSettings->get(MESSAGES_KEY).toBool();
                if (cnSettings->get(MESSAGES_KEY).toBool() != judge)
                    cnSettings->set(MESSAGES_KEY, judge);
                if (appSwitch->isChecked() != judge)
                    appSwitch->setChecked(judge);
                appSwitch->blockSignals(false);
            }
        });

        connect(appSwitch, &KSwitchButton::stateChanged, [=](bool checked) {
            cnSettings->set(MESSAGES_KEY, checked);
            usSettings->set(MESSAGES_KEY, checked);
        });
    }}

void Notice::setHiddenNoticeApp(bool status)
{
    mNoticeAppFrame->setVisible(status);
}

void Notice::setComBoxStatus(bool status)
{
    mOpenTimeHCombox->setEnabled(status);
    mCloseTimeHCombox->setEnabled(status);
    mOpenTimeMCombox->setEnabled(status);
    mCloseTimeMCombox->setEnabled(status);
}

void Notice::loadlist()
{
    QLayoutItem *child;
    while ((child = applistverticalLayout->takeAt(0)) != nullptr)
    {
        child->widget()->setParent(nullptr);
        delete child;
    }
    initOriNoticeStatus();
}
